﻿using Common;
using enterprise.Website.Core.Filter;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace enterprise.Website.Core.ApiControllers
{
    [Route("api/[controller]")]
    [ApiController]
    [LoginAuthorize]
    public class LanguageTextController : ControllerBase
    {
        private readonly SqlContext _sqlContext;
        private readonly ICache _cache;
        public LanguageTextController(SqlContext sqlContext, ICache cache)
        {
            _cache = cache;
            _sqlContext = sqlContext;
        }

        /// <summary>
        /// 获取标签语言管理列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageModel<LanguageText>> Get(string key,string value,int? languageId,int page = 1,int limit = 10)
        {
            Expression<Func<LanguageText, LanguageType, bool>> where = (t, t1) => true;
            if (!key.IsNullOrWhiteSpace())
            {
                where = where.And((t, t1) => t.Key.Contains(key));
            }
            if (languageId.HasValue)
            {
                where = where.And((t, t1) => t.LanguageId == languageId);
            }
            if (!value.IsNullOrWhiteSpace())
            {
                where = where.And((t, t1) => t.Value.Contains(value));
            }
            long Count = 0;
            var list = await _sqlContext.DB.Select<LanguageText, LanguageType>()
                .LeftJoin((t, t1) => t.LanguageId == t1.Id)
                .Where(where)
                .Page(page, limit)
                .Count(out Count)
                .OrderBy((t, t1) => t.LanguageId)
                .ToListAsync((t, t1) => new LanguageText
                {
                    Id = t.Id,
                    Key=t.Key,
                    Value=t.Value,
                    LanguageId=t.LanguageId,
                    LanguageName = t1.Language
                });
            //计算总页数
            int pageCount = (Math.Ceiling(Count.ObjToDecimal() / limit.ObjToDecimal())).ObjToInt();
            return new PageModel<LanguageText>
            {
                count = Count,
                data = list,
                PageSize = limit,
                PageCount = pageCount,
                PageIndex = page,
                code = Count == 0 ? -1 : 0,
                msg = Count == 0 ? "暂无数据" : ""
            };
        }

        /// <summary>
        /// 新增标签
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Post([FromBody] LanguageText data)
        {
            var result = new ApiResult { Success = false, Message = "保存失败" };
            if (data!=null)
            {
                var Id=await _sqlContext.AddAsync(data);
                if (Id > 0)
                {
                    result.Success = true;
                    result.Message = "保存成功";
                }
            }
            return result;
        }

        /// <summary>
        /// 修改标签
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ApiResult> Put(int id, [FromBody] LanguageText data)
        {
            var result = new ApiResult { Success = false, Message = "保存失败" };
            if (data != null)
            {
                var Count = await _sqlContext.UpdateAsync(data);
                if (Count > 0)
                {
                    result.Success = true;
                    result.Message = "保存成功";
                    _cache.Remove($"{SystemConstConfig.LanguageText}_{data.LanguageId}_{data.Key}");
                }
            }
            return result;
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult> Delete(int? id)
        {
            var result = new ApiResult { Success = false, Message = "删除失败" };
            if (id.HasValue)
            {
                var data= _sqlContext.QueryInfo<LanguageText>(m => m.Id == id.Value);
                if (data != null)
                {
                    var Count = await _sqlContext.DeleteAsync<LanguageText>(m => m.Id == id.Value);
                    if (Count > 0)
                    {
                        result.Success = true;
                        result.Message = "删除成功";
                        _cache.Remove($"{SystemConstConfig.LanguageText}_{data.LanguageId}_{data.Key}");
                    }
                }
            }
            return result;
        }
    }
}
